/**
 * Copyright (c) 2013 An Yaming,  All Rights Reserved
 */
package so.shanku.essential.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Paint;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import aym.util.json.JsonMap;
import aym.view.gridview.FixGridLayout;
import so.shanku.essential.R;
import so.shanku.essential.app.MyApplication;
import so.shanku.essential.getdata.JsonKeys;

import java.util.ArrayList;
import java.util.List;

/**
 * 展示商品参数的布局
 *
 * @author 亚明
 */
public class LayoutProductInfoParams2 extends LinearLayout {

    private Context context;
    /**
     * 参数的标题
     */
    private TextView tv_title;
    /**
     * 参数信息的展示
     */
    private LinearLayout pipg_info;
    /**
     * 展示的参数信息
     */
    private List<JsonMap<String, Object>> data;
    /**
     * 当前可选的参数的view集合
     */
    private List<TextView> tvs;
    /**
     * 当前选中的参数的view的索引id
     */
    private int tv_position;
    /**
     * 当前可选的参数的view的单击事件
     */
    private OnClickListener clickListener;

    /**
     * 当前的参数有改变的事件
     */
    private MyOnItemClickListener itemClickListener;

    /**
     * 当前的参数有改变的事件
     */
    public void setItemClickListener(MyOnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    /**
     * 当前选中的参数的view的索引id
     */
    public int getTv_position() {
        return tv_position;
    }

    /**
     * 展示的参数信息
     */
    public List<JsonMap<String, Object>> getData() {
        return data;
    }

    LayoutInflater inflater;

    public LayoutProductInfoParams2(Context context) {
        super(context);
        this.context = context;
        init();
    }

    private void init() {
        clickListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tv_position > -1) {
                    tvs.get(tv_position).setSelected(false);

                }
                for (int i = 0; i < tvs.size(); i++) {
                    if (v.equals(tvs.get(i))) {
                        tv_position = i;
                        tvs.get(tv_position).setSelected(true);
                        if (null != itemClickListener) {
                            itemClickListener.itemClick(tvs.get(i), i);
                        }
                        break;
                    }
                }
            }
        };

        tvs = new ArrayList<TextView>();
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.layout_product_info_params, this);
        tv_title = (TextView) findViewById(R.id.l_p_i_tv_pro_param);
        pipg_info = (LinearLayout) findViewById(R.id.l_p_i_fgl_pro_param);
    }

    /**
     * 设定分类的标题
     *
     * @param title
     */
    public void setTitle(String title) {
        this.tv_title.setText(title);
    }

    /**
     * 设定这个分类下的数据
     */
    public void setParamsData(List<JsonMap<String, Object>> data,
                              int defaultSelectPosition) {
        this.data = data;
        pipg_info.removeAllViews();
        tvs.clear();
        tv_position = -1;
        int containerWidth = MyApplication.getInstance().getScreenWidth() - 2 * context.getResources().getDimensionPixelSize(R.dimen.comon_half_margin)-2*context.getResources().getDimensionPixelSize(R.dimen.common_margin);


        /** 用来测量字符的宽度 */
        Paint paint = new Paint();
        TextView textView = (TextView) inflater.inflate(R.layout.select_goods_params_item, null);
        int itemPadding = textView.getCompoundPaddingLeft() + textView.getCompoundPaddingRight() ;

        paint.setTextSize(textView.getTextSize());

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.setMargins(0, context.getResources().getDimensionPixelSize(R.dimen.comon_half_margin), 0, 0);

        LinearLayout layout = new LinearLayout(context);
        layout.setLayoutParams(params);
        layout.setOrientation(LinearLayout.HORIZONTAL);
        pipg_info.addView(layout);


        //选项item之间的间距
        int params_item_margin= context.getResources().getDimensionPixelSize(R.dimen.goods_params_margin);
        /** 一行剩下的空间 **/
        int remainWidth = containerWidth;

        for (int i = 0; i < data.size(); i++) {
            JsonMap jsonMap = data.get(i);
            String str = jsonMap.getStringNoNull(JsonKeys.Key_ObjName);
            boolean isChecked = jsonMap.getBoolean("IsChecked");
            float itemWidth = paint.measureText(str) + itemPadding;
            if (remainWidth > itemWidth) {
                LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT);
                if(remainWidth > itemWidth+params_item_margin) {
                    tvParams.setMargins(0, 0, params_item_margin, 0);
                } else {
                    tvParams.setMargins(0, 0, 0, 0);
                }

                addItemView(layout, tvParams, str, isChecked, i);
            } else {
                LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT);
                tvParams.setMargins(0, 0, params_item_margin, 0);

                //resetTextViewMarginsRight(layout);
                layout = new LinearLayout(context);
                layout.setLayoutParams(params);
                layout.setOrientation(LinearLayout.HORIZONTAL);
                pipg_info.addView(layout);

                /** 将前面那一个textview加入新的一行 */
                addItemView(layout, tvParams, str, isChecked, i);
                remainWidth = containerWidth;
            }
            remainWidth = (int) (remainWidth - itemWidth + 0.5f-params_item_margin);
        }
        //resetTextViewMarginsRight(layout);
    }

    /*****************
     * 将每行最后一个textview的MarginsRight去掉
     *********************************/
    private void resetTextViewMarginsRight(ViewGroup viewGroup) {
        TextView tempTextView = (TextView) viewGroup.getChildAt(viewGroup.getChildCount() - 1);
        tempTextView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    }

    private void addItemView(ViewGroup viewGroup, LayoutParams params, String text, boolean isChecked, int i) {
        TextView tvItem = (TextView) inflater.inflate(R.layout.select_goods_params_item, null);
        tvItem.setText(text);
        viewGroup.addView(tvItem, params);
        if (isChecked) {
            tvItem.setSelected(true);
            tv_position = i;
        }
        tvs.add(tvItem);
        tvItem.setOnClickListener(clickListener);

    }

    //e

    /**
     * 参数改变的事件
     *
     * @author 亚明
     */
    public interface MyOnItemClickListener {
        void itemClick(View tv, int position);
    }
}
